﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBWebService.Core.Enums;

namespace MBWebService.Core.Classes
{
    public static class SendKeyCommander
    {
        static Dictionary<CommandTypes, SendKey> _keys;

        static Dictionary<CommandTypes, SendKey> Keys
        {
            get
            {
                if (_keys == null)
                    Initialise();
                return _keys;
            }
        }

        /// <summary>
        /// A lot of this code is borrowed from
        /// http://VmcController.codeplex.com, so I can't take credit for this..
        /// </summary>
        private static void Initialise()
        {
            _keys = new Dictionary<CommandTypes, SendKey>();

            // add the keys
            _keys.Add(CommandTypes.Record, new SendKey('R', SpecialKeys.Control));
            _keys.Add(CommandTypes.Left, new SendKey(0x25));
            _keys.Add(CommandTypes.Up, new SendKey(0x26));
            _keys.Add(CommandTypes.Right, new SendKey(0x27));
            _keys.Add(CommandTypes.Down, new SendKey(0x28));
            _keys.Add(CommandTypes.Ok, new SendKey(0x0d));
            _keys.Add(CommandTypes.Back, new SendKey(0x08));
            _keys.Add(CommandTypes.Info, new SendKey('D', SpecialKeys.Control));
            _keys.Add(CommandTypes.ChannelUp, new SendKey(0xbb, SpecialKeys.Control));
            _keys.Add(CommandTypes.ChannelDown, new SendKey(0xbd, SpecialKeys.Control));
            _keys.Add(CommandTypes.DvdMenu, new SendKey('M', SpecialKeys.Control | SpecialKeys.Shift));
            _keys.Add(CommandTypes.DvdAudio, new SendKey('A', SpecialKeys.Control | SpecialKeys.Shift));
            _keys.Add(CommandTypes.DvdSubtitle, new SendKey('U', SpecialKeys.Control | SpecialKeys.Shift));
            _keys.Add(CommandTypes.Cc, new SendKey('C', SpecialKeys.Control | SpecialKeys.Shift));
            _keys.Add(CommandTypes.Pause, new SendKey('P', SpecialKeys.Control));
            _keys.Add(CommandTypes.Play, new SendKey('P', SpecialKeys.Control | SpecialKeys.Shift));
            _keys.Add(CommandTypes.Stop, new SendKey('S', SpecialKeys.Control | SpecialKeys.Shift));
            _keys.Add(CommandTypes.SkipBack, new SendKey('B', SpecialKeys.Control));
            _keys.Add(CommandTypes.SkipForward, new SendKey('F', SpecialKeys.Control));
            _keys.Add(CommandTypes.Rewind, new SendKey('B', SpecialKeys.Control | SpecialKeys.Shift));
            _keys.Add(CommandTypes.FastForward, new SendKey('F', SpecialKeys.Control | SpecialKeys.Shift));
            _keys.Add(CommandTypes.Zoom, new SendKey('Z', SpecialKeys.Control | SpecialKeys.Shift));
            _keys.Add(CommandTypes.Num0, new SendKey(0x60));
            _keys.Add(CommandTypes.Num1, new SendKey(0x61));
            _keys.Add(CommandTypes.Num2, new SendKey(0x62));
            _keys.Add(CommandTypes.Num3, new SendKey(0x63));
            _keys.Add(CommandTypes.Num4, new SendKey(0x64));
            _keys.Add(CommandTypes.Num5, new SendKey(0x65));
            _keys.Add(CommandTypes.Num6, new SendKey(0x66));
            _keys.Add(CommandTypes.Num7, new SendKey(0x67));
            _keys.Add(CommandTypes.Num8, new SendKey(0x68));
            _keys.Add(CommandTypes.Num9, new SendKey(0x69));
            _keys.Add(CommandTypes.Star, new SendKey('3', SpecialKeys.Shift));
            _keys.Add(CommandTypes.Clear, new SendKey(0x1b));
        }

        public static bool SendCommand(CommandTypes type)
        {
            bool retVal = false;

            try
            {
                if (Keys.ContainsKey(type))
                {
                    var sender = Keys[type];
                    sender.Execute();
                    retVal = true;
                }
            }
            catch (Exception ex)
            {
                Logging.Logger.Instance.LogMessage("Error Sending command '{0}'; ERROR: {1}", type, ex.Message);
            }

            return retVal;
        }
    }
}
